﻿using hpcpdi.DataAccess.Base;
using hpcpdi.DataAccess.Interface;
using hpcpdi.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using MySql.Data.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace hpcpdi.DataAccess.Impl
{
    public class TrafficRepository : ITrafficRepository
    {
        private readonly Context context;
        public TrafficRepository(Context _context) {
            context = _context;
        }
       

        /// <summary>
        /// 根据日期获取通行统计量数据
        /// </summary>
        /// <returns></returns>
        public async Task<daily_traffic_volume> GetDailyTrafficVolume(DateTime date)
        {
            //var result =context.Database.SqlQuery<daily_traffic_volume>("select * from daily_traffic_volume where id=(select max(id) from daily_traffic_volume)");

            // int i= result[0].trafficVolume;
            // if (i <= 0)
            // {
            //     i = 0;
            // }
            // return i;
            
            var result =await  context.Daily_traffic_volume.FirstOrDefaultAsync(s => s.tDate == date);
            return result;
        }

        public bool CreateDailyTrafficVolume(daily_traffic_volume dtvModel)
        {
            context.Daily_traffic_volume.Add(dtvModel);
            return context.SaveChanges() > 0;
        }
        public bool UpdateDailyTrafficVolume(daily_traffic_volume dtvModel)
        {

            var item = context.Daily_traffic_volume.SingleOrDefault(s => s.tDate == dtvModel.tDate);
            item.trafficVolume = dtvModel.trafficVolume;
            context.Daily_traffic_volume.Update(item);
            return context.SaveChanges() > 0;
        }

    }


}
